package com.monkey.servicemap.service;

import com.monkey.internalcommon.constant.AmapConfigConstants;
import com.monkey.internalcommon.constant.CommonStatusEnum;
import com.monkey.internalcommon.dto.DicDistrict;
import com.monkey.internalcommon.dto.ResponseResult;
import com.monkey.servicemap.mapper.DicDistrictMapper;
import com.monkey.servicemap.remote.MapDicDistrictClient;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author tao
 * @date 2022/7/4 22:29
 */
@Service
public class DicDistrictService {

    @Autowired
    private MapDicDistrictClient mapDicDistrictClient;

    @Autowired
    private DicDistrictMapper dicDistrictMapper;

    public ResponseResult initDicDistrict(String keywords) {
        // 请求地图
        String dicDistrictResult = mapDicDistrictClient.dicDistrict(keywords);
        JSONObject dicDistrictJsonObject = JSONObject.fromObject(dicDistrictResult);

        int status = dicDistrictJsonObject.getInt(AmapConfigConstants.STATUS);
        if (status != 1) {
            return ResponseResult.fail(CommonStatusEnum.MAP_DISTRICT_ERROR.getCode(), CommonStatusEnum.MAP_DISTRICT_ERROR.getValue());
        }

        JSONArray countryJsonArray = dicDistrictJsonObject.getJSONArray(AmapConfigConstants.DISTRICTS);
        for (int i = 0; i < countryJsonArray.size(); i++) {
            JSONObject countryJsonObject = countryJsonArray.getJSONObject(i);
            String countryAddressCode = countryJsonObject.getString(AmapConfigConstants.ADCODE);
            String countryAddressName = countryJsonObject.getString(AmapConfigConstants.NAME);
            String countryParentAddressCode = "0";
            String countryLevel = countryJsonObject.getString(AmapConfigConstants.LEVEL);

            insertDicDistrict(countryAddressCode, countryAddressName, countryLevel, countryParentAddressCode);

            JSONArray provinceJsonArray = countryJsonObject.getJSONArray(AmapConfigConstants.DISTRICTS);
            for (int p = 0; p < provinceJsonArray.size(); p++) {
                JSONObject provinceJsonObject = provinceJsonArray.getJSONObject(p);
                String provinceAddressCode = provinceJsonObject.getString(AmapConfigConstants.ADCODE);
                String provinceAddressName = provinceJsonObject.getString(AmapConfigConstants.NAME);
                String provinceParentAddressCode = countryAddressCode;
                String provinceLevel = provinceJsonObject.getString(AmapConfigConstants.LEVEL);

                insertDicDistrict(provinceAddressCode, provinceAddressName, provinceLevel, provinceParentAddressCode);

                JSONArray cityJsonArray = provinceJsonObject.getJSONArray(AmapConfigConstants.DISTRICTS);
                for (int c = 0; c < cityJsonArray.size(); c++) {
                    JSONObject cityJsonObject = cityJsonArray.getJSONObject(c);
                    String cityAddressCode = cityJsonObject.getString(AmapConfigConstants.ADCODE);
                    String cityAddressName = cityJsonObject.getString(AmapConfigConstants.NAME);
                    String cityParentAddressCode = provinceAddressCode;
                    String cityLevel = cityJsonObject.getString(AmapConfigConstants.LEVEL);

                    insertDicDistrict(cityAddressCode, cityAddressName, cityLevel, cityParentAddressCode);

                    JSONArray districtsJsonArray = cityJsonObject.getJSONArray(AmapConfigConstants.DISTRICTS);
                    for (int d = 0; d < districtsJsonArray.size(); d++) {
                        JSONObject districtsJsonObject = districtsJsonArray.getJSONObject(d);
                        String districtsAddressCode = districtsJsonObject.getString(AmapConfigConstants.ADCODE);
                        String districtsAddressName = districtsJsonObject.getString(AmapConfigConstants.NAME);
                        String districtsParentAddressCode = cityAddressCode;
                        String districtsLevel = districtsJsonObject.getString(AmapConfigConstants.LEVEL);

                        if (districtsLevel.equals(AmapConfigConstants.STREET)) {
                            continue;
                        }

                        insertDicDistrict(districtsAddressCode, districtsAddressName, districtsLevel, districtsParentAddressCode);
                    }
                }
            }
        }


        return ResponseResult.success();
    }

    public void insertDicDistrict(String addressCode, String addressName, String level, String parentAddressCode) {
        DicDistrict dicDistrict = new DicDistrict();
        dicDistrict.setAddressCode(addressCode);
        dicDistrict.setAddressName(addressName);
        int levelInt = generateLevel(level);
        dicDistrict.setLevel(levelInt);
        dicDistrict.setParentAddressCode(parentAddressCode);
        dicDistrictMapper.insert(dicDistrict);
    }

    public int generateLevel(String level) {
        int levelInt = 0;
        if ("country".equals(level.trim())) {
            levelInt = 0;
        } else if ("province".equals(level.trim())) {
            levelInt = 1;
        } else if ("city".equals(level.trim())) {
            levelInt = 2;
        } else {
            levelInt = 3;
        }
        return levelInt;
    }
}
